<?php

declare(strict_types=1);

namespace App\Controller;

use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\Context\ApplicationContext;
use Hyperf\DbConnection\Db;
use App\Model\Product;
use App\Controller\BaseController;
use Throwable;

#[AutoController]
class ProductController extends BaseController
{
    protected $redis;
    
    public function __construct()
    {
        $container = ApplicationContext::getContainer();
        $this->redis = $container->get(\Hyperf\Redis\Redis::class);
    }
    
    public function index(RequestInterface $request)
    {
        return "Hello World Product";
    }

    //增加商品
    public function addProduct(RequestInterface $request)
    {
        $name = $request->input('name');
        $price = floatval($request->input('price'));
        $num = intval($request->input('num'));
        $is_sale = $request->input('is_sale');
        $start_time = $request->input('start_time');
        $end_time = $request->input('end_time');
        $max_buy = intval($request->input('max_buy'));

        if (empty($name) || empty($price) || empty($num)) {
            $data['err'] = 1;
            $data['msg'] = '参数不能为空';
            return json_encode($data);
        }
        if ($is_sale !== '0' && $is_sale !== '1') {
            $data['err'] = 1;
            $data['msg'] = '抢购参数错误';
            return json_encode($data);
        }
        if ($is_sale === '1') {
            if (empty($start_time) || empty($end_time) || empty($max_buy)) {
                $data['err'] = 1;
                $data['msg'] = '抢购参数不能为空';
                return json_encode($data);
            }
        }
        if ($price <= 0 || $num < 0 || $max_buy < 0) {
            $data['err'] = 1;
            $data['msg'] = '参数错误';
            return json_encode($data);
        }

        Db::beginTransaction();
        try {
            $product = new Product();
            $product->name = $name;
            $product->price = $price;
            $product->num = $num;
            $product->is_sale = $is_sale;
            $product->start_time = $start_time;
            $product->end_time = $end_time;
            $product->max_buy = $max_buy;
            $res = $product->save();
            if ($res) {
                if ($is_sale) {
                    $style = 'product:spike:';
                } else {
                    $style = 'product:normal:';
                }
                $this->redis->hmset($style.$product->id, ['id'=>$product->id,'name'=>$name,'price'=>$price,'num'=>$num,'is_sale'=>$is_sale,'start_time'=>$start_time,'end_time'=>$end_time,'max_buy'=>$max_buy]);
                Db::commit();
                return json_encode(['err' => 0, 'msg' => '添加成功']);
            } else {
                DB::rollback();
                return json_encode(['err' => 1, 'msg' => '添加失败']);
            }
        } catch (Throwable $e) {
            Db::rollBack();
            return json_encode(['err' => 1, 'msg' => $e->getMessage()]);
        }
    }

    //商品详情
    public function ProductDetail(RequestInterface $request)
    {
        $id = $request->input('id');
        $is_sale = $request->input('is_sale');
        if ($is_sale) {
            $product = $this->redis->hgetall('product:spike:'.$id);
        } else {
            $product = $this->redis->hgetall('product:normal:'.$id);
        }
        if (empty($product)) {
            return json_encode(['err' => 1, 'msg' => '商品为空']);
        }
        return ['err' => 0, 'msg' => '获取成功', 'detail' => $product];
    }
}
